/*IPROG - NEW - INI*/
package com.xcustom.commerce.order.commands;

import java.math.BigDecimal;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletResponse;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.efacil.commerce.order.helpers.NotaFiscalEfacilHelper;
import com.ibm.commerce.command.ControllerCommandImpl;
import com.ibm.commerce.datatype.TypedProperty;
import com.ibm.commerce.ejb.helpers.ECConstants;
import com.ibm.commerce.exception.ECException;
import com.xcustom.commerce.vo.NotaFiscalFileType;
import com.xcustom.commerce.vo.NotaFiscalVO;

/**
 * @author commerce
 * Show the user a Pdf or retrieve to download a zip that 
 * contain a xml file, to do this we call eFacil service.
 */
public class GetNotaFiscalCmdImpl extends ControllerCommandImpl implements GetNotaFiscalCmd {
	private final String DEFAULT_FILE_TYPE = "pdf"; 

	@Override
	public void performExecute() throws ECException{	
		
		super.performExecute();

		String fileType = getRequestProperties().getString("type","");
		String filial = getRequestProperties().getString("filial","");
		String nroCliente = getRequestProperties().getString("nroCliente","");
		String intUser = getRequestProperties().getString("intUser","");
		String numNotaFiscal = getRequestProperties().getString("numNotaFiscal","");
		String dateIni = getRequestProperties().getString("dateIni","");
		String dateFim = getRequestProperties().getString("dateFim","");		
		String key = getRequestProperties().getString("key","");	
		NotaFiscalEfacilHelper nfHelper = new NotaFiscalEfacilHelper();
		NotaFiscalFileType nfft;
		
		this.responseProperties = new TypedProperty(); 
		
		if (DEFAULT_FILE_TYPE.equalsIgnoreCase(fileType)){
			nfft =  nfHelper.getPdf(composeNotaFiscalVO(filial,nroCliente,intUser,numNotaFiscal,dateIni,dateFim,key));
				responseProperties.put(ECConstants.EC_CONTENTTYPE, "application/pdf");	
			}else{
				nfft =  nfHelper.getxml(composeNotaFiscalVO(filial,nroCliente,intUser,numNotaFiscal,dateIni,dateFim,key));
				responseProperties.put(ECConstants.EC_CONTENTTYPE, "application/zip");
			}
		
		 byte[] file = nfft.getFile();
		 responseProperties.put(ECConstants.EC_RAWDOCUMENT, file);
		 responseProperties.put(ECConstants.EC_CONTENTLENGTH, new Integer(file.length));
		 responseProperties.put(ECConstants.EC_VIEWTASKNAME, "DirectView");
		 HttpServletResponse httpResponse = (HttpServletResponse) commandContext.getResponse();
		 httpResponse.setHeader("Content-disposition", "inline; filename=" + nfft.getFileName() );
		
	}
	
	private NotaFiscalVO composeTestNotaFiscalVOPDF(){
		//Obtenemos un pdf a partir de estos daatos
		try {
			GregorianCalendar gcal = new GregorianCalendar();
			XMLGregorianCalendar fec;
			fec = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
			fec.setDay(23);fec.setMonth(10);fec.setYear(2013);
			fec.setHour(00);fec.setMinute(0);fec.setSecond(0);	
			
			NotaFiscalVO nfVO = new NotaFiscalVO();
				nfVO.setFilial(369);
				nfVO.setNroCliente(new BigDecimal(124466));
				nfVO.setIntUser(-1);
				nfVO.setNumNotaFiscal(551432);
				nfVO.setDateIni(fec);
				nfVO.setDateFim(fec);
				nfVO.setKey("");
		
	return nfVO;		
		} catch (DatatypeConfigurationException e1) {
			
			e1.printStackTrace();
		}
		return null;
	}
	
	private NotaFiscalVO composeTestNotaFiscalVOXML(){
		//Obtenemos un pdf a partir de estos daatos
		try {
			GregorianCalendar gcal = new GregorianCalendar();
			XMLGregorianCalendar fec;
			fec = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
			fec.setDay(30);fec.setMonth(10);fec.setYear(2013);
			fec.setHour(00);fec.setMinute(0);fec.setSecond(0);	
			
			NotaFiscalVO nfVO = new NotaFiscalVO();
				nfVO.setFilial(99);
				nfVO.setNroCliente(new BigDecimal(177140));
				nfVO.setIntUser(-1);
				nfVO.setNumNotaFiscal(424714);
				nfVO.setDateIni(fec);
				nfVO.setDateFim(fec);
				nfVO.setKey("");
	return nfVO;		
		} catch (DatatypeConfigurationException e1) {
			
			e1.printStackTrace();
		}
		return null;
	}
	/**
	 * @author commerce
	 * @param filial
	 * @param nroCliente
	 * @param intUser
	 * @param numNotaFiscal
	 * @param dateIni
	 * @param dateFim
	 * @param key
	 * @return NotaFiscalVO
	 * compose a NotaFiscalVO (logical representation - Nota Fiscal)
	 * */
	private NotaFiscalVO composeNotaFiscalVO(String filial,String nroCliente,String intUser ,String numNotaFiscal,String dateIni,String dateFim, String key){

			XMLGregorianCalendar dIni = null , dFim= null;			
			try {
				dIni = 	DatatypeFactory.newInstance().newXMLGregorianCalendar(dateIni);
				dFim = 	DatatypeFactory.newInstance().newXMLGregorianCalendar(dateFim);
			} catch (DatatypeConfigurationException e) {
			
				e.printStackTrace();
			}
			
			NotaFiscalVO nfVO = new NotaFiscalVO();
				nfVO.setFilial(Integer.parseInt(filial));
				nfVO.setNroCliente(new BigDecimal(nroCliente));
				nfVO.setIntUser(-1);
				nfVO.setNumNotaFiscal(Integer.parseInt(numNotaFiscal));
				nfVO.setDateIni(dIni);
				nfVO.setDateFim(dFim);
				nfVO.setKey(key);
				
	return nfVO;		
	}
}
/*
  ...CMDREG...
INSERT INTO COMMERCE.CMDREG (STOREENT_ID, INTERFACENAME, DESCRIPTION, CLASSNAME, PROPERTIES, LASTUPDATE, TARGET, OPTCOUNTER)
VALUES (0, 'com.xcustom.commerce.order.commands.GetNotaFiscalCmd', NULL, 'com.xcustom.commerce.order.commands.GetNotaFiscalCmdImpl', NULL, NULL, 'Local', NULL);
*/
/*
 Test.
 https://localhost/webapp/wcs/stores/servlet/GetNotaFiscalCmd?catalogId=10001&storeId=12253&langId=-1
 * */
/*IPROG - NEW - END*/